package P150;

import java.util.*;

/**
 * @author LiangMu
 * @version \$Id: P44.java, v 0.1, 2024年07月06日 02:25 LiangMu Exp $$
 */
public class P44 {

    public static int longestConsecutive(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }

        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }

        List<Integer> ints = new ArrayList<>(set);
        ints.sort(Integer::compare);

        int res = 1;
        int count = 1;
        for (int i = 1; i < ints.size(); i++) {
            if (ints.get(i) - ints.get(i - 1) == 1) {
                count++;
            } else {
                res = Math.max(res, count);
                count = 1;
            }
        }
        res = Math.max(res, count);

        return res;
    }


    public static void main(String[] args) {
        int[] ints = {0,3,7,2,5,8,4,6,0,1};
        System.out.println(longestConsecutive(ints));
    }
}
